package com.weixiaodong.javabasic.Java中的枚举类型.后台用户角色管理.单值枚举;

public class Main {

    /**
     * switch中可以使用枚举类型
     * @param role
     */
    public static void JudgeRole(UserRole role) {
        switch (role) {
            case ROLE_NORMAL:
                System.out.println("普通用户");
                break;
            case ROLE_ROOT_ADMIN:
                System.out.println("系统管理员");
                break;
            case ROLE_ORDER_ADMIN:
                System.out.println("订单管理员");
                break;
        }
    }


    public static void main(String[] args) {
        UserRole role1 = UserRole.ROLE_ROOT_ADMIN;
        UserRole role2 = UserRole.ROLE_ORDER_ADMIN;
        UserRole role3 = UserRole.ROLE_NORMAL;

        // 1. 返回指定枚举的集合
        UserRole[] values = UserRole.values();
        for (UserRole user : values) {
            System.out.println(user);
        }

        System.out.println("+++++++++++++++++++++++++++");

        // 2. 返回枚举的常量序, 这个序就是枚举类中定义的先后顺序
        System.out.println(role1.ordinal()); // 0
        System.out.println(role2.ordinal()); // 1
        System.out.println(role3.ordinal()); // 2

        System.out.println("+++++++++++++++++++++++++++");

        // 3. 枚举之间的比较
        System.out.println(role1.compareTo(role2)); // -1
        System.out.println(role2.compareTo(role3)); // -1
        System.out.println(role3.compareTo(role1)); // 2

        // 4. 返回枚举的值
        System.out.println(role1.name());
        System.out.println(role2.name());
        System.out.println(role3.name());

        // 5. 返回指定名称的常量
        System.out.println(UserRole.valueOf("ROLE_ROOT_ADMIN"));
        System.out.println(UserRole.valueOf("ROLE_ORDER_ADMIN"));
        System.out.println(UserRole.valueOf("ROLE_NORMAL"));

        JudgeRole(UserRole.ROLE_NORMAL);
    }
}
